package com.yuke.cloud.service.opc.controller.rpc;

import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.opc.dto.UploadFile;
import com.yuke.cloud.service.api.opc.service.OpcFileFeignApi;
import com.yuke.cloud.service.opc.service.OpcFileService;
import com.yuke.cloud.service.opc.utils.FastDFSClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

@Api(value = "API - OpcFileFeignClient", tags = "处理File相关请求的API", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Slf4j
@RestController
@RequestMapping
public class OpcFileFeignClient  extends BaseController implements OpcFileFeignApi {
    @Autowired
    private OpcFileService opcFileService;

    @Override
    public Wrapper<Integer> fileDelete(@RequestBody List<UploadFile> uploadFiles) {
        log.info("----- Enter '/image' fileDelete() -----");
        int status = 0;
        try {
            for (UploadFile uploadFile : uploadFiles) {
                //-1：失败；0：成功；2：已被删除
                String filePath = uploadFile.getFilePath();
                List<String> strs = Arrays.asList(filePath.split("/"));
                //group1/M00/00/00/rBMBRFwhjH2ATKuLAAD8-W0gOiE441.jpg
                String groupName = strs.get(0);
//                String remoteFileName = filePath.substring(7);
                StringBuffer stringBuffer = new StringBuffer();
                for (int i = 1; i < strs.size(); i++) {
                    stringBuffer.append(strs.get(i));
                    if (i != strs.size() - 1)
                        stringBuffer.append("/");
                }
                String remoteFileName = stringBuffer.toString();
                log.info("--- groupName is {}, remoteFileName is {} ---", groupName, remoteFileName);
                status += FastDFSClient.deleteFile(groupName, remoteFileName);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return WrapMapper.error(e.getMessage());
        }
        log.info("----- Leave '/image' fileDelete() -----");
        return WrapMapper.ok(status);
    }

    @Override
    public Wrapper<Integer> filesDelete(@RequestBody UploadFile uploadFile) {
        log.info("----- Enter '/image' filesDelete() -----");
        List<String> filePaths = uploadFile.getFilePaths();
        int status = 0;
        try {
            for (String filePath : filePaths) {
                //-1：失败；0：成功；2：已被删除
                List<String> strs = Arrays.asList(filePath.split("/"));
                //group1/M00/00/00/rBMBRFwhjH2ATKuLAAD8-W0gOiE441.jpg
                String groupName = strs.get(0);
//                String remoteFileName = filePath.substring(7);
                StringBuffer stringBuffer = new StringBuffer();
                for (int i = 1; i < strs.size(); i++) {
                    stringBuffer.append(strs.get(i));
                    if (i != strs.size() - 1)
                        stringBuffer.append("/");
                }
                String remoteFileName = stringBuffer.toString();
                log.info("--- groupName is {}, remoteFileName is {} ---", groupName, remoteFileName);
                status += FastDFSClient.deleteFile(groupName, remoteFileName);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return WrapMapper.error(e.getMessage());
        }
        log.info("----- Leave '/image' filesDelete() -----");
        return WrapMapper.ok(status);
    }

//    @Override
//    @ApiOperation(httpMethod = "POST", value = "UploadFile uploadFile")
//    public String uploadExcelFiles(InputStream inputStream,  String fileName, String ext) {
//        //log.info("----- UploadFile uploadFile -----");
//        String path = "uploadFile";
//        try {
//            path = opcFileService.saveFiles(inputStream,fileName,ext);
//        } catch (Exception e) {
//            log.error("Upload file failed",e);
//        }
//        return path;
//    }
}
